package com.huangyi.rabbitmq_amqp.routing;

import com.huangyi.rabbitmq_amqp.config.Constants;
import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Producer {
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(Constants.HOST);
        factory.setPort(Constants.PORT);
        factory.setVirtualHost(Constants.VIRTUAL_HOST);
        factory.setUsername(Constants.USER);
        factory.setPassword(Constants.PASSWORD);

        Connection connection = factory.newConnection();
        Channel channel  = connection.createChannel();

        channel.queueDeclare(Constants.ROUTING_QUEUE1,true,false,false,null);
        channel.queueDeclare(Constants.ROUTING_QUEUE2, true, false, false, null);
        channel.exchangeDeclare(Constants.ROUTING_EXCHANGE, BuiltinExchangeType.DIRECT,true);

        channel.queueBind(Constants.ROUTING_QUEUE1,Constants.ROUTING_EXCHANGE,"a");
        channel.queueBind(Constants.ROUTING_QUEUE2,Constants.ROUTING_EXCHANGE,"b");
        channel.queueBind(Constants.ROUTING_QUEUE2,Constants.ROUTING_EXCHANGE,"c");

        String msg = "hello routing_queue 1, routing key is a";
        channel.basicPublish(Constants.ROUTING_EXCHANGE,"a",null,msg.getBytes());
        String msg2 = "hello routing_queue 2, routing key is b";
        channel.basicPublish(Constants.ROUTING_EXCHANGE,"b", MessageProperties.PERSISTENT_TEXT_PLAIN,msg2.getBytes());
        String msg3 = "hello routing_queue 3, routing key is c";
        channel.basicPublish(Constants.ROUTING_EXCHANGE,"c",null,msg3.getBytes());

        System.out.println("生产信息完毕");
        channel.close();
        connection.close();
    }

}
